package com.rq.base.net;


import com.rq.base.BuildConfig;
import com.rq.base.net.impl.HttpInnerListener;
import com.rq.base.net.impl.HttpTagListener;
import com.rq.base.util.LOG;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HttpSender {

    public static void send(final String path, String way, Map<String, String> head, Map<String, Object> param,
                            final HttpInnerListener listener, final Object tag) {
        innerPost(path, way, head, param, new HttpInnerListener() {
            @Override
            public void onString(String con) {
                LOG.e("HttpSender", path + ">   onString.BHMManager:" + con);
                if (listener != null) listener.onString(con);
                if (listener instanceof HttpTagListener) {
                    LOG.e("HttpSender", "HttpTagListener.51:" + tag);
                    ((HttpTagListener) listener).onTag(tag);
                }
            }

            @Override
            public void onEmptyResponse(String e) {
                LOG.e("HttpSender.BEAN", "onEmptyResponse:" + e);
                if (listener instanceof HttpTagListener) {
                    LOG.e("HttpSender", "HttpTagListener.59:" + tag);
                    ((HttpTagListener) listener).onTag(tag);
                }
                if (listener != null) listener.onEmptyResponse(e);
                LOG.e("HTTP", "onEmptyResponse.219:");
            }
        });
    }

    private static Set<String> waitUrls = new HashSet<>();
    private static Set<String> debugRemove = new HashSet<>();//调试期间不打印内容
    private static Set<String> appRemove = new HashSet<>();

    public static void removeKeep(List<String> urls) {
        if (urls != null) {
            appRemove.addAll(urls);
        }
    }

    public static void stopLog(String... url) {
        if (url != null) {
            debugRemove.addAll(Arrays.asList(url));
        }
    }

    public final static String TAG_NET_WAIT = "please.wait.net.response";

    //访问网络获取数据
    private static void innerPost(final String url, String way, Map<String, String> head, final Map<String, Object> params,
                                  final HttpInnerListener listener) {
        final String tag = url.substring(url.lastIndexOf("/") + 1);
        final String keepUrl = url.substring(url.lastIndexOf(":"));
        if (waitUrls.contains(keepUrl)) {
            return;
        }
        LOG.e("BEAN." + tag + ".POST.url", "=============<" + url + ">=============");
        if (BuildConfig.DEBUG && !debugRemove.contains(keepUrl) && !url.contains("wirtelog"))
            LOG.bean(tag + "." + way, params);
        HttpThreadManager.get().send(url, way, head, params, new HttpThreadManager.OnListener() {
            @Override
            public void onString(String json) {
                LOG.e("HttpSender", "onString.84:");
                if (listener != null) listener.onString(json);
                waitUrls.remove(url);
                if (BuildConfig.DEBUG && !debugRemove.contains(keepUrl) && !url.contains("wirtelog"))
                    LOG.bean(tag, json);
            }

            @Override
            public void onEmptyResponse(String errorMsg) {
                LOG.e("HttpSender", "onEmptyResponse.91:");
                if (listener != null) listener.onEmptyResponse(errorMsg);
                waitUrls.remove(url);
            }

            @Override
            public void onOver() {
                waitUrls.remove(url);
            }
        });
    }

    /**
     * 封装请求体信息
     *
     * @param params:请求体内容
     * @param encode:编码格式
     * @return
     */
    static StringBuffer getRequestData(Map<String, Object> params, String encode) {
        if (params == null || params.size() == 0) return new StringBuffer();
        //存储封装好的请求体信息
        StringBuffer stringBuffer = new StringBuffer();
        try {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (entry.getValue() == null) continue;
                stringBuffer.append(entry.getKey()).append("=")
                        .append(URLEncoder.encode(String.valueOf(entry.getValue()), encode)).append("&");
            }
            //删除最后的一个"&"
            if (stringBuffer.toString().length() > 1)
                stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer;
    }
}

